from sqlalchemy.orm import Session
from models import Contribution
from utils import allocate_project_profits
from sqlalchemy.orm import Session
from models import Contribution, SubCompany

def update_contributions(db: Session, user_id: int, company_id: int, new_value: float):
    contribution = db.query(Contribution).filter_by(user_id=user_id, company_id=company_id).first()
    if contribution:
        contribution.value = new_value
    else:
        contribution = Contribution(user_id=user_id, company_id=company_id, value=new_value)
        db.add(contribution)
    db.commit()

    # Update contribution values for all users in the company
    sub_company = db.query(SubCompany).get(company_id)
    total_value = sum(c.value for c in sub_company.contributions)
    for c in sub_company.contributions:
        c.value = (c.value / total_value) * sub_company.market_cap

    # Distribute contribution gains using quantum computing and voting
    distribute_contribution_gains(db, company_id)

def distribute_contribution_gains(db: Session, company_id: int):
    # Generate random contribution distribution using quantum computing
    sub_company = db.query(SubCompany).get(company_id)
    allocate_project_profits(db,project_id=sub_company.id)